//
//  ArcView.m
//  Horn
//
//  Created by CA on 15-1-15.
//  Copyright (c) 2015年 CA. All rights reserved.
//

#import "ArcView.h"
#import "PNChart.h"


static const CGFloat kArcPointerTag = 1000;

@interface ArcView ()
@property (nonatomic, strong) PNCircleChart *circleChart;
@end

@implementation ArcView

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self addCircleChart];
    }
    return self;
}

#pragma mark - Getter/Setter

- (void)setTotal:(NSNumber *)total
{
    _total = total;
    self.circleChart.total = total;
}

- (void)setStrokeColor:(UIColor *)strokeColor
{
    _strokeColor = strokeColor;
    self.circleChart.strokeColor = strokeColor;
}

- (void)setCurrent:(NSNumber *)current
{
    _current = current;
    self.circleChart.current = current;
}

- (void)setDuration:(NSTimeInterval)duration
{
    _duration = duration;
    self.circleChart.duration = duration;
}

- (void)setLineWidth:(NSNumber *)lineWidth
{
    _lineWidth = lineWidth;
    self.circleChart.lineWidth = lineWidth;
}

- (UICountingLabel *)countingLabel
{
    return self.circleChart.countingLabel;
}

- (void)setTextSize:(CGFloat)textSize
{
    _textSize = textSize;
    
    self.circleChart.countingLabel.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:textSize];
}

- (void)setCircleBackgroundColor:(UIColor *)circleBackgroundColor
{
    _circleBackgroundColor = circleBackgroundColor;
    
    self.circleChart.circleBackground.strokeColor = circleBackgroundColor.CGColor;
}

- (void)setIsOrignalCircleChart:(BOOL)isOrignalCircleChart
{
    _isOrignalCircleChart = isOrignalCircleChart;
    if (_isOrignalCircleChart) {
        [self addOriginalCircleChart];
    }
}

#pragma mark - Private Method

- (void)addOriginalCircleChart
{
    if (self.circleChart != nil) {
        [self.circleChart removeFromSuperview];
    }
    
    self.circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) total:@300 current:@0 clockwise:YES shadow:NO startAngle:0.0f endAngle:360.0f];
    
    self.circleChart.backgroundColor = [UIColor clearColor];
    self.circleBackgroundColor = [UIColor colorWithHex:0x74B3EE alpha:0.4];
    self.lineWidth = @7;
    self.circleChart.countingLabel.textColor = kOrangeColor;
    self.circleChart.countingLabel.format = @"%d";
    self.circleChart.countingLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:25.0f];
    self.circleChart.circleBackground.lineCap = kCALineCapRound;
    self.circleChart.circle.lineCap = kCALineCapRound;
    
    self.circleChart.strokeColor = [UIColor colorWithHex:0x74B3EE];
    
    [self addSubview:self.circleChart];
}

- (void)addCircleChart
{
    self.circleChart = [[PNCircleChart alloc] initWithFrame:CGRectMake(0, -0.5, self.bounds.size.width, self.bounds.size.height + 12) total:@300 current:@0 clockwise:YES shadow:NO startAngle:142.0f endAngle:38.0f];
    
    self.circleChart.backgroundColor = [UIColor clearColor];
    self.circleChart.lineWidth = @4;
    self.circleChart.countingLabel.textColor = kOrangeColor;
    self.circleChart.countingLabel.format = @"%d";
    self.circleChart.countingLabel.font = [UIFont systemFontOfSize:15];
    self.circleChart.circle.lineCap = kCALineCapSquare;
    self.circleChart.strokeColor = kOrangeColor;
    
    [self addSubview:self.circleChart];
}

- (void)addArcPointer
{
//    UIImageView *pointerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(3, 4, 90, 90)];
//    
//    pointerImageView.contentMode = UIViewContentModeTopLeft;
//    pointerImageView.image = [UIImage imageNamed:@"sport_icon_pointer"];
//    pointerImageView.tag = kArcPointerTag;
//    [self addSubview:pointerImageView];
}

- (void)strokeChart
{
    [self.circleChart strokeChart];
}

- (void)growChartByAmount:(NSNumber *)growAmount
{
    [self.circleChart growChartByAmount:growAmount];
}

- (void)updateChartByCurrent:(NSNumber *)current
{
    [self.circleChart updateChartByCurrent:current];
    [self.circleChart.countingLabel countFrom:[_current integerValue] to:[current integerValue] withDuration:self.duration];
    _current = current;
    
    CGFloat percent = [current floatValue]/[self.total floatValue];
//    NSLog(@"percent: %f", percent);
    CGFloat rotationAngle = percent * 1.7 * M_PI;
    
    UIImageView *pointerImageView = (UIImageView *)[self viewWithTag:kArcPointerTag];
    if (pointerImageView != nil) {
        [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            
            pointerImageView.transform = CGAffineTransformMakeRotation(rotationAngle);
            
        } completion:nil];
    }
    
}


@end
